/*****************************************************************************
 *   Copyright 2011 ZHUANG Yuan              	                             *
 *   SudokuITS as Final Year Project for BScCS, City University of Hong Kong *
 *                                      									 *
 *   Licensed under the Apache License, Version 2.0 (the "License");         *
 *   you may not use this file except in compliance with the License.        *
 *   You may obtain a copy of the License at                                 *
 *                                                                           *
 *   http://www.apache.org/licenses/LICENSE-2.0                              *
 *                                                                           *
 *   Unless required by applicable law or agreed to in writing, software     *
 *   distributed under the License is distributed on an "AS IS" BASIS,       *
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.*
 *   See the License for the specific language governing permissions and     *
 *   limitations under the License.                                          *
 *****************************************************************************/

package android.sudoku.its.views;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.sudoku.its.sqlite.SQLiteHandler;
import android.sudoku.its.sqlite.StudentTable;
import android.sudoku.its.student.Student;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;

/**
 * Login Page
 * @author 51158282
 *
 */
public class LoginPage extends Activity implements OnClickListener {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		// Set layout for Login Page
		setContentView(R.layout.login);
		
		// Set onClickListener for buttons
		ImageView loginButton = (ImageView) findViewById(R.id.login_page_login_button);
		loginButton.setOnClickListener(this);
		ImageView cancelButton = (ImageView) findViewById(R.id.login_page_cancel_button);
		cancelButton.setOnClickListener(this);
	}
	
	public void onClick(View v) {
		switch(v.getId()) {
			case R.id.login_page_login_button:
				if (ifUserAuthenticated())
					startMainMenuPage();
				else 
					displayAlert();
				break;
			case R.id.login_page_cancel_button:
				this.finish();
				break;
			}		
	}
	
	/**
	 * Check if the student is authenticated 
	 * @return		A boolean variable indicating if the student is authenticated 
	 */
	private boolean ifUserAuthenticated() {
		boolean ifUserAuthenticated = false;
		
		// Get user input name
		EditText nameTV = (EditText) findViewById(R.id.login_page_name_text);
		String name = nameTV.getText().toString();
		
		// Get user input password
		EditText pwdTV = (EditText) findViewById(R.id.login_page_password_text);
		String pwd = pwdTV.getText().toString();
		
		SQLiteHandler sqliteHandler = new SQLiteHandler(this);
		StudentTable studentTable = new StudentTable();
		
		Student student = studentTable.getStudentByName(sqliteHandler, name);
		String pwd_retrieved = student.getPassword();
		
		if (pwd.equals(pwd_retrieved))
			ifUserAuthenticated = true;
		
		return ifUserAuthenticated;
	}
	
	/**
	 * Go to Main Menu Page
	 */
	private void startMainMenuPage() {		
		// Get user input name
		EditText nameTV = (EditText) findViewById(R.id.login_page_name_text);
		String name = nameTV.getText().toString();
		
		Intent i =  new Intent(getBaseContext(), MainMenuPage.class);
		i.putExtra(MainMenuPage.USER_TITLE, name);
		startActivity(i);
	}
	
	/**
	 * Display an alert dialogue to remind the student about incorrect name or password
	 */
	private void displayAlert() {
		String alertMsg = "Your user name or password is incorrect. Please try again.";
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setMessage(alertMsg);
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {			
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				
			}
		});
		alertDialog.show();
	}

}
